/*
 * This file is part of Ember.
 *
 * Ember is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Ember is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Ember.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Grant Tyra 2010
 * granttyra@gmail.com
*/

#include "ember_list.h"
#include <string.h>
#include <stdlib.h>

EmberList *EmberListCreate(void) {
	EmberList *list = malloc(sizeof(EmberList));
	list->root = NULL;
	list->size = 0;
	return list;
}

void EmberListDestroy(EmberList *list) {
	EmberListNode *current = list->root;
	EmberListNode *next = NULL;

	while (current != NULL) {
		next = current->next;
		free(current);
		current = next;
	}

	free(list);
}

int EmberListAdd(EmberList *list, void *value) {
	EmberListNode *tail = NULL;
	EmberListNode *node = NULL;
	int i = 0;

	if (list == NULL) {
		return -1;
	}

	node = malloc(sizeof(EmberListNode));
	node->data = value;
	node->next = NULL;

	if (list->root == NULL) {
		list->root = node;
	} else {
		tail = list->root;

		for (i = 1; i < list->size; i++) {
			tail = tail->next;
		}

		tail->next = node;
	}

	list->size++;
	return list->size - 1;
}

int EmberListSet(EmberList *list, int index, void *value) {
	EmberListNode *current = NULL;
	int i = 0;

	if (list == NULL || index >= list->size) {
		return -1;
	}

	current = list->root;

	for (i = 0; i < index; i++) {
		current = current->next;
	}

	current->data = value;
	return index;
}

int EmberListRemove(EmberList *list, int index) {
	EmberListNode *previous = NULL;
	EmberListNode *current = NULL;
    int i = 0;

	if (list == NULL || index >= list->size) {
		return -1;
	}

	current = list->root;

	if (index == 0) {
		list->root = current->next;
		free(current);
		list->size--;
		return list->size;
	}

	for (i = 0; i < index; i++) {
		previous = current;
		current = current->next;
	}

	previous->next = current->next;
	free(current);

	list->size--;
	return list->size;
}

int EmberListFind(EmberList *list, void *value) {
	EmberListNode *current = NULL;
	int index = 0;

	if (list == NULL) {
		return -1;
	}

	current = list->root;

	while (current != NULL) {
		if (current->data == value) {
			return index;
		}

		current = current->next;
		index++;
	}

	return -1;
}

void *EmberListGet(EmberList *list, int index) {
	EmberListNode *current = NULL;
	int i = 0;

	if (list == NULL || index >= list->size) {
		return NULL;
 	}

	current = list->root;

	for (i = 0; i < index; i++) {
		current = current->next;
	}

	return current->data;
}
